`
https://leetcode.cn/problems/single-element-in-a-sorted-array/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var singleNonDuplicate = function (nums) {
  const n = nums.length

  let left = -1, right = n
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (nums[mid] !== nums[mid - 1] && nums[mid] !== nums[mid + 1]) {
      return nums[mid]
    }
    const targetIdx = mid % 2 === 0 ? mid + 1 : mid - 1
    if (nums[mid] === nums[targetIdx]) {
      left = mid
    } else {
      right = mid
    }
  }
};